# -*- makefile -*-
# Boot Makefile

# Add future prerequisites somewhere among the list of libraries.
# You really should not have to add anything in the $(LD) line.
#
# Don't link against libgcc because of -mregparm=3. The library
# would only be necessary for 64-bit arithmetics. These functions
# can be replaced by using div32/mod32 from lib/libk (and should
# for efficency reasons).

boot_img.o:	boot_img.x2 boot_img.ren_syms
	$(LINK_MESSAGE)
	$(VERBOSE)$(OBJCOPY) --redefine-syms=boot_img.ren_syms $< $@

boot_img.ren_syms: boot_img.x2
	$(LINK_MESSAGE)
	$(VERBOSE)$(NM) -u $^ | perl -n -e 'if (/^\s*U\s+bootstrap_(.*)$$/) { print "bootstrap_$$1 $$1\n"; }' > $@

boot_img.x1:	$(OBJ_BOOT) $(DRIVERS) $(LIBC) 
	$(LINK_MESSAGE)
	$(VERBOSE)$(LD) $(LDFLAGS) $(LD_EMULATION_FLAG) -r -s -o $@ \
	-T $(srcdir)/boot/ia32/bootstrap.ld $^

boot_img.x2:	boot_img.x1
	$(LINK_MESSAGE)
	$(VERBOSE)$(OBJCOPY) --prefix-alloc-sections=.bootstrap --prefix-symbols=bootstrap_ -G bootstrap__boot_start $^ $@


-include .kernel.ia32.lds.d

# $(1) = fiasco - variable
# $(2) = fiasco.debug - variable
# $(3) = Additional $(OBJ_X), e.g. OBJ_<unittest>
define gen_kernel_rules =
$(2):	kernel.ia32.lds boot_img.o $$(CRT0) $$(OBJ_KERNEL) $(3) $$(JDB) $$(KERNEL_EXTRA_LIBS) $$(ABI) $$(JABI) libdrivers.a $$(LIBUART) $$(LIBK) $$(CXXLIB) $$(LIBC) libfonts.a libgluedriverslibc.a
		$$(LINK_MESSAGE)
		$$(VERBOSE)$$(LD) $$(LDFLAGS) $$(LD_EMULATION_FLAG) -N -o $$@ \
		   -T $$< -gc-sections $$(filter-out $$<,$$+) \
		   $$(KERNEL_UNRES_SYMS)
		$$(call ADD_CONFIGFILE,$$@)

$(1): $(2)
		$$(LINK_MESSAGE)
		$$(VERBOSE)$$(STRIP) -o $$@ $$<
		$$(VERBOSE)$$(OBJCOPY) --add-gnu-debuglink=$$< $$@
		$$(VERBOSE)chmod 755 $$@
endef

# generate fiasco and fiasco.debug rules
$(eval $(call gen_kernel_rules,$(BOOT),$(KERNEL),$(OBJ_STDWORKLOAD)))

BOOT_CLEAN := boot_img.x1 boot_img.x2 boot_img.ren_syms boot_img.ren_syms kernel.ia32.lds
